package com.match.competition_role.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.match.commonutils.Result;
import com.match.competition_role.client.RoleClient;
import com.match.competition_role.entity.CompetitionRole;
import com.match.competition_role.entity.CompetitionUserRole;
import com.match.competition_role.service.CompetitionRoleService;
import com.match.competition_role.service.CompetitionUserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author AdiosF
 * @since 2021-10-02
 */
@Api(tags = "角色模块")
@RestController
@RequestMapping("/admin/competition_role/competition-role")
public class CompetitionRoleController {
       @Autowired
       private CompetitionRoleService competitionRoleService;
       @Autowired
       private CompetitionUserRoleService competitionUserRoleService;
       @Autowired
       private RoleClient roleClient;

       @Autowired
       private CompetitionUserController competitionUserController;


       @ApiOperation("添加角色")
       @PostMapping("/addRole")
       public Result addRole(@ApiParam(required = true,value = "角色名称")
                        @RequestBody CompetitionRole competitionRole){
           boolean save = competitionRoleService.save(competitionRole);
           if (!save){
               return Result.error().message("角色保存失败");
           }
           return Result.ok();
       }


       //TODO(教师多角色情况)
       @ApiOperation("根据角色Id删除角色")
       @DeleteMapping("/deleteRoleById/{roleId}")
       public Result deleteRoleById(@ApiParam(required = true,value = "角色ID")
                               @PathVariable(value = "roleId")String roleId){
           //首先根据角色id删除中间表信息
           QueryWrapper<CompetitionUserRole> competitionUserRoleQueryWrapper=new QueryWrapper<>();
           competitionUserRoleQueryWrapper.eq("role_id",roleId);
           List<CompetitionUserRole> list = competitionUserRoleService.list(competitionUserRoleQueryWrapper);
           //封装教师职工号
           List<String> stringList=new ArrayList<>();
           if (!list.isEmpty()){
               for (CompetitionUserRole competitionUserRole : list) {
                   stringList.add(competitionUserRole.getUserId());
               }
           }

           //重新为教师分配教师角色
           for (String teacherNumber : stringList) {
               com.match.entity.Vo.UserRoleVo userRoleVo=new com.match.entity.Vo.UserRoleVo();
               //roleId这时为角色名称不是角色Id
               userRoleVo.setRoleId("教师");
               userRoleVo.setTeacherNumber(teacherNumber);
               roleClient.updateUserRoleByNumber(userRoleVo);
           }

           //更新教师角色为角色
           QueryWrapper<CompetitionRole> competitionRoleQueryWrapper=new QueryWrapper<>();
           competitionRoleQueryWrapper.eq("role_name","教师");
           CompetitionRole one = competitionRoleService.getOne(competitionRoleQueryWrapper);
           //重新为教师分配教师角色
           for (String teacherNumber : stringList) {
               com.match.entity.Vo.UserRoleVo userRoleVo1=new com.match.entity.Vo.UserRoleVo();
               //roleId这时为角色Id
               userRoleVo1.setRoleId(one.getId());
               userRoleVo1.setTeacherNumber(teacherNumber);
               competitionUserController.updateUserRoleById(userRoleVo1);
           }
           boolean b = competitionRoleService.removeById(roleId);
           if (!b){
               return Result.error().message("角色移除失败");
           }
           return Result.ok().message("角色移除成功");
       }


       @ApiOperation("查找所有角色")
       @GetMapping("/getAllRole")
       public Result getAllRole(){
           QueryWrapper<CompetitionRole> competitionRoleQueryWrapper=new QueryWrapper<>();
           competitionRoleQueryWrapper.orderByAsc("gmt_create");
           List<CompetitionRole> list = competitionRoleService.list(competitionRoleQueryWrapper);
           if (list.isEmpty()){
               return Result.error().message("暂无角色");
           }
           return Result.ok().data("roleList",list);
       }

       @ApiOperation("根据查询条件得到对应角色")
       @GetMapping("/getOneRoleByQuery/{roleName}")
       public Result getOneRoleByQuery(@PathVariable(value = "roleName")String roleName){
           QueryWrapper<CompetitionRole> competitionRoleQueryWrapper=new QueryWrapper<>();
           competitionRoleQueryWrapper.eq("role_name",roleName);
           CompetitionRole one = competitionRoleService.getOne(competitionRoleQueryWrapper);
           return Result.ok().data("role",one);
       }


       @ApiOperation("更改角色信息")
       @PostMapping("/updateRoleById")
       public Result updateRoleById(@RequestBody CompetitionRole competitionRole){
           boolean b = competitionRoleService.updateById(competitionRole);
           if (!b){
               return Result.error().message("角色信息更改失败");
           }
           return Result.ok().message("角色信息更改成功");
       }
}

